home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK2.toast / Development Kits (Disc 2) / Open Transport / Sample Code / Network Watch (DMZ) 1.5 / sources / dMZLists.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-25  |  14.2 KB  |  623 lines  |  [TEXT/MPS ]

  1. /*
  2. #-------------------------------------------------------------------------------------------
  3. #
  4. #    Program:    < DMZ 1.3 >
  5. #    File:        < dmzLists.c >
  6. #    
  7. #    by pvh
  8. #    of <Apple Macintosh Developer Technical Support - or wheverever>
  9. #
  10. #    Modification History
  11. #    5/27/94  rrk     Made PPC compatible
  12. #    9/21/92  rrk    Got rid of "^2 ^3" string left in dialog at init time
  13. #                    if DMZ started on system with no zones.  Put in paramText
  14. #                    call in tellUserNoZones()
  15. #
  16. #    Copyright © 1990 Apple Computer, Inc.
  17. #    All rights reserved.
  18. #    
  19. #-------------------------------------------------------------------------------------------
  20. */
  21.  
  22. #include    "dmz.h"
  23.  
  24. /* 
  25.  *    External globals we use in this dude.
  26.  */
  27. extern     DialogPtr         gMyDialog;
  28. extern     long            gSortMode;
  29. extern  EventRecord        gMyEvent;
  30. extern  short            gATalkTransFlag;
  31. extern  myATQEntry        gATQEntry;
  32. /* 
  33.  *    Globals we use here
  34.  */
  35. ListHandle                 gZonesList, gObjectTypeList;
  36. char                     gNameGlob[34];
  37.  
  38.  
  39. /* 
  40.  *    This is a simple routine that invalidates the passed in dialog item's rectangle 
  41.  *    so that we get a cleaner refresh of only the items that need updating.
  42.  */
  43. void invalidateItem(short whichItem)
  44. {
  45.     Rect         r;
  46.     short         kind;
  47.     Handle         h;
  48.     GrafPtr     savedPort;
  49.     
  50.     GetPort(&savedPort);
  51.     SetPort(gMyDialog);
  52.     GetDialogItem(gMyDialog, whichItem, &kind, &h, &r);
  53.     InvalRect(&r);
  54.     BeginUpdate(gMyDialog);
  55.     UpdateDialog(gMyDialog, gMyDialog->visRgn);
  56.     EndUpdate(gMyDialog);
  57.     SetPort(savedPort);
  58. }
  59.     
  60.     
  61. void DisposOfMyLists()
  62. {
  63.     LDispose(gZonesList);
  64.     LDispose(gObjectTypeList);
  65. }
  66.     
  67. void OpenZonesList()
  68. {
  69.     Rect         dBounds, rView;
  70.     Point         cSize;
  71.     GrafPtr     tp;
  72.     short        kind;
  73.     Handle        h;
  74.     
  75.     GetPort(&tp);
  76.     SetPort(gMyDialog);
  77.     GetDialogItem(gMyDialog, 1, &kind, &h, &rView);
  78.  
  79.     rView.top += 1;
  80.     rView.bottom -= 1;
  81.     rView.right -= 16;
  82.  
  83.     cSize.v = 0;
  84.     cSize.h = 0;
  85.  
  86.     SetRect(&dBounds, 0, 0, 1, 0); 
  87.     gZonesList = LNew(&rView, &dBounds, cSize, 0, gMyDialog, true, false, false, true);
  88.     (**gZonesList).selFlags = lUseSense+lOnlyOne;
  89.     SetPort(tp);
  90. }
  91.  
  92. void tellUserNoZones()
  93. {
  94.     Cell             theCell;
  95.     short         ignore, i;
  96.     GrafPtr     tp;
  97.     
  98.     ClearZoneCells();        // quick way to clear out an old entry - sort of overkill
  99.     GetPort(&tp);
  100.     SetPort(gMyDialog);
  101.  
  102.     LSetDrawingMode(false, gZonesList);
  103.  
  104.     i = 0;
  105.  
  106.     theCell.v = i;
  107.     theCell.h = 0;
  108.  
  109.     ignore = LAddRow(1, i, gZonesList);    
  110.  
  111.     LClrCell(theCell, gZonesList);
  112.     if (((dmzEntryPtr) *(gATQEntry.globs))->atalkActive)  // If ATalk active.
  113.     {
  114.         LSetCell("No zones <*>.", PStrLen("\pNo zones <*>."), theCell, gZonesList);    
  115.         ParamText("\p", "\p", "\p", "\p");
  116.     }
  117.     else
  118.     {
  119.         LSetCell("AppleTalk is off.", PStrLen("\pAppleTalk is off."), theCell, gZonesList);    
  120.         ParamText("\p", "\p", "\p", "\p");
  121.     }
  122.  
  123.     LSetDrawingMode(true, gZonesList);
  124.     InvalRect(&(**gZonesList).rView);
  125.     SetPort(tp);
  126. }
  127.  
  128. /*****************************************************************************/
  129.  
  130.  
  131.  
  132. /* Compare two pascal-strings. */
  133.  
  134. #pragma segment StringUtils
  135. short    pcmp(StringPtr s1, StringPtr s2)
  136. {
  137.     short    i, len;
  138.     char    j;
  139.  
  140.     if ((len = s1[0]) > s2[0]) len = s2[0];
  141.  
  142.     for (i = 1; i <= len; ++i)
  143.         if (j = s1[i] - s2[i]) return(j);
  144.  
  145.     return(s1[0] - s2[0]);
  146. }
  147.     
  148.  
  149. /* 
  150.  *    compare routine for qsort().  it uses a global selector for the different 'fields' inthe list
  151.  *    they are actually offsets to the data in each cell.
  152.  *    OK...so it's really gross.
  153.  */
  154. int myCompare(const void *aStr, const void *bStr)
  155. {
  156.     long    sortPtr;
  157.     int        result;
  158.     
  159.     switch(gSortMode) {
  160.         case sortOnZoneName:
  161.              sortPtr = myNetworkEntityObject;
  162.              break;
  163.         case sortOnType:
  164.              sortPtr = myNetworkEntityType;
  165.              break;
  166.         case sortOnNet:
  167.              sortPtr = myNetworkEntityNet;
  168.              break;
  169.         case sortOnNode:
  170.              sortPtr = myNetworkEntityNode;
  171.              break;
  172.         case sortOnSocket:
  173.              sortPtr = myNetworkEntitySocket;
  174.              break;
  175.         }
  176.     result = IUCompString((ConstStr255Param)((long)aStr + sortPtr), (ConstStr255Param)((long)bStr + sortPtr));
  177.     return result;
  178. //    return(IUCompString((void *)(aStr + sortPtr), (void *)(bStr + sortPtr)));
  179. }
  180.  
  181. /* 
  182.   *    call qsort() 
  183.   */
  184. void letsSort(Ptr theBuffPtr, short numZonesGot, short number)
  185. {        
  186.     SetPort(gMyDialog);
  187.     
  188.     /* tell them we're sorting */
  189.     ParamText("\p", "\p", "\p", "\psorting...");        
  190.     invalidateItem(kProgressID);
  191.         
  192.     /* do the sort */
  193. #ifdef __MWERKS__
  194.     qsort(theBuffPtr, (size_t)numZonesGot, (size_t)number, (_Cmpfun *)myCompare);
  195. #else
  196.     qsort(theBuffPtr, (size_t)numZonesGot, (size_t)number, myCompare);
  197. #endif
  198.     
  199.     /* erase the info text */
  200.     ParamText("\p", "\p", "\p", "\p");        
  201.     invalidateItem(kProgressID);
  202. }
  203.  
  204.  
  205. /* 
  206.  *    set up the zone list 
  207.  */
  208. void SetZoneCells(Ptr bufferPtr, short NumZonesGot)
  209. {
  210.     Cell         theCell;
  211.     short         ignore, i;
  212.     long         bufferIndex;
  213.     GrafPtr     tp;
  214.     Str32        myZone, zoneString;
  215.         
  216.     myZone[0] = 0;
  217.     getMyZone((char *)myZone);
  218.     
  219.     GetPort(&tp);
  220.     SetPort(gMyDialog);
  221.  
  222.     /* added 5/5/89 pvh */
  223.     gSortMode = sortOnZoneName;
  224.     letsSort(bufferPtr, NumZonesGot, zoneNameSize);
  225.     
  226.     LSetDrawingMode(false, gZonesList);
  227.     
  228.     bufferIndex = 0L;
  229.     i = 0;
  230.     while(i<NumZonesGot) {
  231.         ignore = LAddRow(1, i, gZonesList);    
  232.         theCell.v = i;
  233.         theCell.h = 0;
  234.         
  235.         SpinTheCursor();        
  236.         zoneString[0] = (char)(bufferPtr+bufferIndex)[0];
  237.         LSetCell((Ptr)bufferPtr+bufferIndex + 1L, zoneString[0], theCell, gZonesList);    
  238.  
  239.         /*
  240.          *    when we find our zone, select it in the list
  241.          */
  242.         zoneString[0] = (char)(bufferPtr+bufferIndex)[0];
  243.         BlockMove((Ptr)bufferPtr+bufferIndex + 1L, (Ptr)&zoneString + 1L, zoneString[0]);
  244.         if(EqualString(zoneString, myZone, false, false)) {
  245.             LSetSelect(true, theCell, gZonesList);
  246.             LAutoScroll(gZonesList);
  247.             getTypesNamesInZone((char *)zoneString);
  248.             }
  249.             
  250.         i += 1;
  251.         bufferIndex += zoneNameSize;
  252.         }
  253.  
  254.     LSetDrawingMode(true, gZonesList);
  255.  
  256.     SetPort(tp);
  257.  
  258. }
  259.  
  260. /* 
  261.  *    clear the zone list 
  262.  */
  263. void ClearZoneCells()
  264. {
  265.     GrafPtr     tp;
  266.     Str32        tempStr;
  267.  
  268.     GetPort(&tp);
  269.     SetPort(gMyDialog);
  270.     
  271.     ParamText("\p", "\p", "\p", "\p");    // clear the number of items text which appears in item 7
  272.  
  273.             // clear out the "number of found items" dialog item
  274.     tempStr[0] = 0;        
  275.     setItemString(gMyDialog, (short) kObjectCountID, tempStr);
  276.  
  277.     LSetDrawingMode(false, gZonesList);
  278.     
  279.     LDelRow(0,0, gZonesList);
  280.     EraseRect(&(**gZonesList).rView);
  281.  
  282.     LSetDrawingMode(true, gZonesList);
  283.     InvalRect(&(**gZonesList).rView);
  284.  
  285.     SetPort(tp);
  286.  
  287. }
  288.  
  289. /* 
  290.  *    handle click in the zone list box 
  291.  */
  292. void doZonesListStuff()
  293. {
  294.     Boolean     tempBool;
  295.     Cell         thisCell;
  296.     Rect         scrollRect;
  297.     GrafPtr     tp;
  298.     Str255          dataPtr;
  299.     short         dataLen;
  300.     Point        localPoint = gMyEvent.where;
  301.  
  302.     ListHandle    testH;
  303.     
  304.     thisCell.v = 0;
  305.     thisCell.h = 0;
  306.     GetPort(&tp);
  307.     SetPort(gMyDialog);
  308.     
  309.     if(gMyDialog != FrontWindow())
  310.         SelectWindow(gMyDialog);
  311.     else 
  312.     {
  313.         if (!((dmzEntryPtr) *(gATQEntry.globs))->atalkActive)  // ATalk not active.
  314.             return;
  315.         GlobalToLocal(&localPoint);
  316.         scrollRect = (**gZonesList).rView;
  317.         scrollRect.left = (**gZonesList).rView.right;
  318.         scrollRect.right = scrollRect.left + 15;
  319.         if(PtInRect(localPoint, &scrollRect))
  320.             tempBool = LClick(localPoint, gMyEvent.modifiers, gZonesList);
  321.         else if(PtInRect(localPoint, &(**gZonesList).rView)) {
  322.             if(LClick(localPoint, gMyEvent.modifiers, gZonesList)) {
  323.                 testH = gObjectTypeList;    
  324.                 LGetSelect(true, &thisCell, gZonesList);
  325.                 dataLen = 255;
  326.                 LGetCell(&dataPtr[1], &dataLen, thisCell, gZonesList);
  327.                 dataPtr[0] = dataLen;
  328.                 BlockMove(&dataPtr, &gNameGlob[0], sizeof(gNameGlob));
  329.                 getTypesNamesInZone((char *)dataPtr);
  330.                 }
  331.             else {
  332.                 LGetSelect(true, &thisCell, gZonesList);
  333.                 dataLen = 255;
  334.                 LGetCell(&dataPtr[1], &dataLen, thisCell, gZonesList);
  335.  
  336.                 dataPtr[0] = dataLen;
  337.  
  338.                 BlockMove(&dataPtr[0], &gNameGlob[0], sizeof(gNameGlob));
  339.                 }
  340.             }
  341.         }
  342.     SetPort(tp);
  343. }
  344.  
  345. void OpenObjectTypeList()
  346. {
  347.     Rect         dBounds, rView;
  348.     Point         cSize;
  349.     GrafPtr     tp;
  350.     Handle        h;
  351.     short        kind, ignore;
  352.     
  353.     GetPort(&tp);
  354.     SetPort(gMyDialog);
  355.     GetDialogItem(gMyDialog, 2, &kind, &h, &rView);
  356.  
  357.     rView.top += 1;
  358.     /*
  359.     // rView.bottom -= 1;
  360.     // rView.bottom = gMyDialog->portRect.bottom-16;
  361.     */
  362.     
  363.     rView.right -= 16;
  364.  
  365.     cSize.v = 0;
  366.     cSize.h = 0;
  367.  
  368.     SetRect(&dBounds, 0, 0, 1, 0); /* two cells across by 0 down extra cell for hiding entity net address */
  369.     gObjectTypeList = LNew(&rView, &dBounds, cSize, 128, gMyDialog, true, false, false, true);
  370.     (**gObjectTypeList).selFlags = lUseSense+lOnlyOne;
  371.     
  372.     SizeControl((**gObjectTypeList).vScroll, (**(**gObjectTypeList).vScroll).contrlRect.right - 
  373.         (**(**gObjectTypeList).vScroll).contrlRect.left, (**(**gObjectTypeList).vScroll).contrlRect.bottom - 
  374.         (**(**gObjectTypeList).vScroll).contrlRect.top - 16);
  375.     
  376.     ignore = LAddColumn(1, 0, gObjectTypeList);    
  377.  
  378.     SetPort(tp);
  379. }
  380.  
  381. void doObjectDoubleClick()
  382. {
  383.     Cell                 thisCell;
  384.     short                dataLen;
  385.     long                temp;
  386.     myNetworkEntity        myEnt;
  387.     AddrBlock            address;
  388.  
  389.     thisCell.v = 0;
  390.     thisCell.h = 0;
  391.     LGetSelect(true, &thisCell, gObjectTypeList);
  392.     
  393.     /* 
  394.      *    at this point do whatever you want with the object selection.
  395.      *    send 'em lots of packets and try and kill them or whatever...
  396.      */
  397.     dataLen = sizeof(myNetworkEntity);
  398.     LGetCell((Ptr) &myEnt, &dataLen, thisCell, gObjectTypeList);
  399.  
  400.     StringToNum((void *) myEnt.net, (long *) &temp);
  401.     address.aNet = (short) temp;
  402.     StringToNum((void *) myEnt.node, (long *) &temp);
  403.     address.aNode = (short) temp;
  404.     StringToNum((void *) myEnt.socket, (long *) &temp);
  405.     address.aSocket = (short) temp;
  406.     if (OpenTransportActive())
  407.         doOTEcho(&myEnt);
  408.     else
  409.         doEcho(&myEnt);
  410. }
  411.  
  412.  
  413. void doObjectTypeListStuff()
  414. {
  415.     Boolean             ignore;
  416.     Cell                 thisCell;
  417.     Rect                 scrollRect;
  418.     GrafPtr             tp;
  419.     Point                localPoint = gMyEvent.where;
  420.     
  421.     thisCell.v = 0;
  422.     thisCell.h = 0;
  423.     GetPort(&tp);
  424.     SetPort(gMyDialog);
  425.     
  426.     if(gMyDialog != FrontWindow())
  427.         SelectWindow(gMyDialog);
  428.     else {
  429.         if (!((dmzEntryPtr) *(gATQEntry.globs))->atalkActive)  // ATalk not active.
  430.             return;
  431.         GlobalToLocal(&localPoint);
  432.         scrollRect = (**gObjectTypeList).rView;
  433.         scrollRect.left = (**gObjectTypeList).rView.right;
  434.         scrollRect.right = scrollRect.left + 15;
  435.         if(PtInRect(localPoint, &scrollRect))
  436.             ignore = LClick(localPoint, gMyEvent.modifiers, gObjectTypeList);
  437.         else if(PtInRect(localPoint, &(**gObjectTypeList).rView)) {
  438.             /*
  439.              *    Check for a double click.
  440.              */
  441.              if(LClick(localPoint, gMyEvent.modifiers, gObjectTypeList)) {
  442.                 doObjectDoubleClick();
  443.                 }
  444.             }
  445.         }
  446.     SetPort(tp);
  447. }
  448.  
  449. /* 
  450.  *    clear the object and types list 
  451.  */
  452. void ClearObjectTypesList()
  453. {
  454.     GrafPtr     tp;
  455.     
  456.     GetPort(&tp);
  457.     SetPort(gMyDialog);
  458.  
  459.     LSetDrawingMode(false, gObjectTypeList);
  460.     
  461.     LDelRow(0,0, gObjectTypeList);
  462.     EraseRect(&(**gObjectTypeList).rView);
  463.  
  464.     LSetDrawingMode(true, gObjectTypeList);
  465.     InvalRect(&(**gObjectTypeList).rView);
  466.  
  467.     SetPort(tp);
  468.  
  469. }
  470.  
  471. /* 
  472.  *    this routine pads strings out to various lengths.  justification to right or left can 
  473.  *    be specified.  we need this for a cleaner display as well as insuring a correct sort
  474.  *    on numeric data (i.e. the strings '1234' and ' 1234' are not equivalant, but if
  475.  *    we justify to the right, we'll always be OK.  (whoops maybe not in the case of the Japanese
  476.  *    alphabet...damn...
  477.  */
  478. void padEntry(unsigned char *entry, short length, short just)
  479. {
  480.     unsigned char     index, i;
  481.     
  482.     switch(just) {
  483.         case leftJust:
  484.             index = entry[0];    /* get length byte */
  485.             index += 1;            /* increment by 1 */
  486.         
  487.             while(index <= length) {
  488.                 entry[index] = ' ';
  489.                 index += 1;
  490.                 }    
  491.             break;
  492.         case rightJust:
  493.             index = entry[0];    /* get length byte */
  494.         
  495.             if(index<length) {
  496.                 BlockMove(&entry[1], (Ptr)&entry[1]+length-index, (long)index);
  497.                 
  498.                 i = 1;
  499.                 while(index < length) {
  500.                     entry[i] = ' ';
  501.                     i += 1;
  502.                     index += 1;
  503.                     }    
  504.                 }
  505.             break;
  506.         }
  507.     entry[0] = length;
  508. }
  509.     
  510. /* 
  511.  *    this is called when the NBPLookup has finished.  the buffer and the number of obects found
  512.  *    is added to the list.
  513.  */
  514. void SetObjectTypeCells(Ptr bufferPtr, short numDevicesGot)
  515. {
  516.     Cell                         theCell;
  517.     short                     ignore;
  518.     short                 numDevicesIndex;
  519.     GrafPtr             tp;
  520.     AddrBlock             address;
  521.     EntityName            abEntity;
  522.     unsigned char        charHolder[6];
  523.     long                g;
  524.     myNetworkEntity     myNetEnt;
  525.     Ptr                    newBuffer;
  526.     char                tempStr[10];
  527.     
  528.     /* tell the user this may take a little while... */
  529.     /*waitAWhile = GetCursor(watchCursor);
  530.     SetCursor(*waitAWhile);*/
  531.     
  532.     GetPort(&tp);
  533.     SetPort(gMyDialog);
  534.  
  535.     /* make room for as many objects as we need */
  536.     newBuffer = NewPtr(numDevicesGot*sizeof(myNetworkEntity));
  537.     if(newBuffer == 0L)
  538.         return;
  539.         
  540.     LDelRow(0, 0, gObjectTypeList);    /* deletes lists's cells */
  541.     
  542.     LSetDrawingMode(false, gObjectTypeList);  /* turn drawing off */
  543.     
  544.     numDevicesIndex = 0;
  545.     
  546.     while(numDevicesIndex<numDevicesGot) {
  547.         theCell.v = numDevicesIndex;
  548.         
  549.         ignore = LAddRow(1, numDevicesIndex, gObjectTypeList);    
  550.  
  551.         NBPExtract(bufferPtr, numDevicesGot, numDevicesIndex+1, &abEntity, &address);
  552.  
  553.         /* first move address data into "hidden" cell */
  554.         theCell.h = 1;
  555.         LSetCell((Ptr) &address, 4, theCell, gObjectTypeList);
  556.         
  557.         /* now move object name & type into one cell */
  558.         theCell.h = 0;
  559.         
  560.         /* object */
  561.         BlockMove(&abEntity.objStr, &myNetEnt.object, 33L);    
  562.         if(myNetEnt.object[0] > 32)
  563.             myNetEnt.object[0] = 32;
  564.             
  565.         /* type */
  566.         BlockMove(&abEntity.typeStr, &myNetEnt.type, 33L);
  567.         if(myNetEnt.type[0] > 32)
  568.             myNetEnt.type[0] = 32;
  569.         
  570.         /* network */
  571.         g = (long)address.aNet;
  572.         g = g & 0x0000FFFF; /* mask out hiword crap */
  573.         NumToString(g, (void *) &charHolder);
  574.         padEntry((void *) &charHolder, 5, rightJust);
  575.         BlockMove(&charHolder, &myNetEnt.net, 6L);
  576.  
  577.         /* node */
  578.         NumToString((long)address.aNode, (void *) &charHolder);
  579.         padEntry((void *) &charHolder, 3, rightJust);
  580.         BlockMove(&charHolder, &myNetEnt.node, 4L);
  581.     
  582.         /* socket */
  583.         NumToString((long)address.aSocket, (void *) &charHolder);
  584.         padEntry((void *) &charHolder, 3, rightJust);
  585.         BlockMove(&charHolder, &myNetEnt.socket, 4L);
  586.                 
  587.         BlockMove((Ptr) &myNetEnt, (Ptr)newBuffer+(numDevicesIndex)*sizeof(myNetworkEntity), 
  588.             sizeof(myNetworkEntity));
  589.  
  590.         LSetCell((Ptr)&myNetEnt, sizeof(myNetworkEntity), theCell, gObjectTypeList);    
  591.  
  592.         numDevicesIndex += 1; 
  593.         }
  594.  
  595.     /* do a quicksort() on the mess */
  596.     letsSort(newBuffer, numDevicesGot, sizeof(myNetworkEntity));
  597.     
  598.     numDevicesIndex = 0;
  599.     
  600.     while(numDevicesIndex<numDevicesGot) {
  601.         theCell.v = numDevicesIndex;
  602.         theCell.h = 0;
  603.         BlockMove((Ptr)newBuffer+(numDevicesIndex)*sizeof(myNetworkEntity), (Ptr) &myNetEnt, sizeof(myNetworkEntity));
  604.         LSetCell((Ptr)&myNetEnt, sizeof(myNetworkEntity), theCell, gObjectTypeList);    
  605.         numDevicesIndex += 1;
  606.         }
  607.         
  608.     DisposePtr(newBuffer);
  609.     
  610.     NumToString(numDevicesIndex, (void *) tempStr);
  611.  
  612.     ParamText((ConstStr255Param)tempStr, "\p# of objects: ", "\p", "\p");
  613.  
  614.     LSetDrawingMode(true, gObjectTypeList);
  615.     invalidateItem(8);
  616.     invalidateItem(2);
  617.  
  618.     SetPort(tp);
  619.     InitCursor();
  620. }
  621.  
  622.  
  623.